DMelt:Plots/1 Canvases

From HandWiki
Member

Graphical canvases

Every data container of DataMelt can either be

  • plottable (extends the class jhplot.Plottable jhplot.Plottable and can be shown on the canvases.
  • not plottable, i.e. represent pure data. Almost all non-plottable objects they can be converted to plottable objects

You can check if the object is plottable if it implements the method //'is3D//' (it is 3D object or 2D). Here is the example:

>>from jhplot import *
>>p=P1D()
>>print p.is3D()

It returns false (not a 3D object), i.e. it belongs to objects which can only plotted on 2D canvaces.

>>from jhplot import *
>>p=PND() # 2D array
>>print p.is3D()

You will see the error "AttributeError: 'jhplot.PND' object has no attribute 'is3D'.

Plottable objects can be shown on canvaces. Below we summarize the DataMelt canvases which can be used to display data, functions and other graphical objects (axes, labels, keys, arrows) using Java/Jython:

Canvas Description
jhplot.Plot jhplot.Plot Plot data and functions using MatLab syntax. Can be used to plot X-Y array,jhplot.P1D jhplot.P1D,hplot.F1D hplot.F1D.
jhplot.HPlot jhplot.HPlot 2D canvas and contour plots for jhplot.H1D jhplot.H1D,jhplot.P1D jhplot.P1D, hplot.F1D hplot.F1D objects and other graphical primitives. jhplot.H2D jhplot.H2D can be shown as a desity plot.
jhplot.HPlotter jhplot.HPlotter a similar 2D canvas but has a different look and feel since originate from the JAS (FreeHEP). It has also support for jhplot.H1D jhplot.H1D, jhplot.P1D jhplot.P1D, hplot.F1D hplot.F1D. jhplot.H2D jhplot.H2D can be shown as a density plot or contour plots (several styles). Plot regions are more interactive than for the HPlot class and there is a better integration with Jaida/FreeHEP libraries
jhplot.HPlotJas jhplot.HPlotJas 2D canvas based on Jas2. Accepts jhplot.H1D jhplot.H1D, jhplot.P1D jhplot.P1D, hplot.F1D hplot.F1D. jhplot.H2D jhplot.H2D for plotting. Useful for interactive rebinning of 1d array and interactive non-linear regression (fits)
jhplot.HPlotJa jhplot.HPlotJa 2D canvas with interactive editor for drawing diagrams. It has also support for jhplot.H1D jhplot.H1D, jhplot.P1D jhplot.P1D, hplot.F1D hplot.F1D as well as for graphical primitives and Feynman diagrams
jhplot.HPlotChart jhplot.HPlotChart Used to embed charts from JFreeChart and allows for export to vector formats (EPS, PDF)
jhplot.HPlotXY jhplot.HPlotXY 2D canvas with interactive editor for drawing diagrams. It has also support for jhplot.H1D jhplot.H1D, jhplot.P1D jhplot.P1D, hplot.F1D hplot.F1D. Plots can have separate pads
jhplot.SPlot jhplot.SPlot a light-weight 2D canvas with a support for jhplot.H1D jhplot.H1D, jhplot.P1D jhplot.P1D, hplot.F1D hplot.F1D as well as for the standard Java arrays
jhplot.WPlot jhplot.WPlot canvas based on Waterloo with support for jhplot.H1D jhplot.H1D, jhplot.P1D jhplot.P1D, hplot.F1D hplot.F1D. Plots can be done as bars, scatter, lines, dots. Polar coordinates are supported.
jhplot.HPlot2D jhplot.HPlot2D is used for contour (or density) plots in 2D for classes jhplot.H2D jhplot.H2D, jhplot.P2D jhplot.P2D, jhplot.F2D jhplot.F2D
jhplot.HPlot3D jhplot.HPlot3D is used for plotting data in 3D jhplot.H2D jhplot.H2D, jhplot.P3D jhplot.P3D, jhplot.F2D jhplot.F2D
jhplot.HPlotXYZ jhplot.HPlotXYZ is used for plotting data and geometrical objects in 3D using OpenGL. Can be used to show jhplot.H2D jhplot.H2D, jhplot.P3D jhplot.P3D, jhplot.F2D jhplot.F2D
jhplot.HPlot3DP jhplot.HPlot3DP is used for plotting parametric and non-parametric functions such as jhplot.FPR jhplot.FPR
jhplot.HPlotMX jhplot.HPlotMX for plotting parametric and non-parametric functions
jhplot.HChart jhplot.HChart is used for showing 2D and 3D charts, lines, functions, histograms. Also has a support for jhplot.P1D jhplot.P1D and H1D histograms. This canvas supported by the Android version of DataMelt.
jhplot.HGraph jhplot.HGraph is used for showing interconnected graphs.
jhplot.HPlotRT jhplot.HPlotRT to show data in real time as traces
jhplot.HVisAd jhplot.HVisAd to show data in 2D/3D for visualization and analysis of numerical data using VisAd VisAd
org.jlab.groot.ui.TCanvas org.jlab.groot.ui.TCanvas to show data in 2D using GROOT visualization (similar to PyROOT)
jhplot.HZirkel jhplot.HZirkel is used for interactive geometry

In addition, one can use several external libraries for plotting data:

Library Description
JFreeChart JFreeChart To plot data and charts
Waterloo scientific library Waterloo scientific library To plot data in chart using interactive canvases
Gral scientific library Gral scientific library To plot data in chart using interactive canvases

Such large number of canvases is mainly due to the fact that they are designed for somewhat different tasks and originate from different packages. But the good thing is that thy often share same methods and constructors, so changing the style of the plots can be simply done by replacing the constructor keeping the same plotting methods.

General features

Despite the large number of supported canvases, all methods used to create interactive plots are rather similar. For most examples to be discussed, we will use the HPlot canvas, but very similar methods also can be used for HPlotter, HPlotJa etc. canvases.

Typically, you can build a canvas like this:

from jhplot *
c1 = HPlot("Canvas",600,400,2,1) # canvas size 600x400, 2 plot regions
c1.visible()         # make it visible
c1.setAutoRange()    # autorange for X
c1.draw(object1)     # draw object1 (H1D,F1D,P1D etc)
c1.draw(object2)     # draw a new object
c1.export("figure.pdf") # export to PDF file

This code create a canvas with the size 600x400 (in pixels), it has 2 pads to show data. The method visible() makes the canvas visible. If you do not want the pop-up image, just call "visible(False)". Then you can draw any mathematical object or data. Then you can export the image to vector format. Now you are ready to plot functions, histograms and datasets. See documentation of jhplot.HPlot jhplot.HPlot class. It should be noted the HPlot canvas can be replaced by any other canvas described above.

The example also creates image "figure.pdf". Please read Exporting Canvases section.

Several plots regions

These canvases can be used to show several pads (plot regions) with plotted objects. Specify the number of divisions in X and Y in the constructors. The navigate to a specific plot region using the method "cd()". Here is example of how to create 2x2 pads on a single canvas:

from jhplot  import *

c1 = HPlot("Canvas",500,400,2,2)
c1.visible()
c1. setRangeAll(0,10,0,10)
h1 = P1D("Simple")

c1.cd(1,1)
h1.add(5,6);  c1.draw(h1)

c1.cd(1,2)
h1.add(4,3); c1.draw(h1)

c1.cd(2,1)
h1.add(3,3); c1.draw(h1)

c1.cd(2,2)
h1.add(2,1); c1.draw(h1)
c1.export ("example.pdf") # export to PDF

The output image is:

DMelt example: Shwoing X-Y data on 4 canvas regions

This works for jhplot.HPlot jhplot.HPlot, jhplot.HPlotJa jhplot.HPlotJa, jhplot.HPlot3D jhplot.HPlot3D and many other pads.

Here we use the same X and Y ranges. One can use "setAutoRange()" (after each "cd()") method to set autorange for each pad. Also, try "setRange(xmin,xmax,ymin,ymax)" to set fixed ranges for each pads. it shows 4 pads with data points.

The plots are fully customizable, i.e. one can change any attribute (axis, label, ticks, ticks label). This will be shown later.

Exporting to images

When DMelt functions, histograms and data structures are shown, one can create an image using the menu [File]-[Export]. One can also save image in a file using the method "export" to a variety of vector graphics formats as well as bitmap image formats.

In the example below, we save an instance of the HPlot class to a PDF file:

c1.export('image.pdf')

we export drawings on the canvas HPlot (c1 represents its instance) into a PDF file. One can also export it into SVG, PNG, JPEG, EPS, PS etc. formats using the appropriate extension for the output file name.

As example, simply append the statement "c1.export('image.ps')" at the end of code snippets.

Images can be generated in a background (without a pop-up frame with the canvas). For this, use the method "c1.visible(0)", where "0" means Java false.

Please read Export to vector graphics section for more information how to deal with high-quality images for publications.